//
//  Engine.h
//  Void Dead
//
//  Created by Sidney Just on 02.07.09.
//  Copyright 2009 by Sidney Just
//  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
//  either version 3 of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//  

#import <Foundation/Foundation.h>
#import "panelManager.h"
#import "TextureManager.h"
#import "CollisionManager.h"
#import "labelManager.h"
//#import "Contentmanager.h"

@class VD_ContenManager;

typedef enum {
	kDisplayNot,
	kDisplayUpLeft,
	kDisplayUpRight,
	kDisplayDownRight,
	kDisplayDownLeft
} kFPSLabelDisplayMode;

/**
 * This class provides a view and a render context, it also provides the VD Managers.
 **/

@interface VD_Engine : UIView {
@public
	/**
	 * The color that will used by glClear
	 **/
	VD_Color *skyColor;
	
    /* The pixel dimensions of the backbuffer */
    GLint backingWidth;
    GLint backingHeight;
	
	VD_TextureManager *texManager;
	VD_PanelManager *panManager;
	VD_CollisionManager *colManager;
	VD_labelManager *lblManager;
	VD_ContenManager *contentManager;
	
	NSTimeInterval timeStep;
	
	EAGLContext *context;
    
    /* OpenGL names for the renderbuffer and framebuffers used to render to this view */
    GLuint viewRenderbuffer, viewFramebuffer;
	
	kFPSLabelDisplayMode FPSLabelDisplayMode;
    
	int visiblePolygons;
	int drawCalls;
	int verticesCount;
	
@private	
	id *CCTarget;
	SEL *CCSelector;
	int targets;
	
	id *CCWaitTarget;
	SEL *CCWaitSelector;
	int waits;
	
	BOOL forcedRendering;
	
	VD_label *fpsLabel;
	VD_label *debugLabel;
    
    /* OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist) */
    GLuint depthRenderbuffer;
    
    NSTimer *animationTimer;
    NSTimeInterval animationInterval;
}

@property (nonatomic, retain) EAGLContext *context;
@property GLuint viewRenderbuffer;
@property GLuint viewFramebuffer;

/**
 * Contains the pointer to the VD_ContentManager created by the Engine
 **/
@property (nonatomic, retain) VD_ContenManager *contentManager;

/**
 * Count of all rendered polygons in the last scene
 **/
@property int visiblePolygons;

/**
 * Count of all glDraw commands fired in the last scene
 **/
@property int drawCalls;

/**
 * Count of rendered vertices in the last scene
 **/
@property int verticesCount;

/**
 * Stores the time that was needed for the last frame
 * Available after the first drawView call.
 * You can use this property to get the FPS by calculating 1/timeStep. You can also use this to create framerate independent movement.
 **/
@property NSTimeInterval timeStep;

/**
 * Determines if the Engine displays a FPS and Info label or not.
 * Values: kDisplayNot = The Engine display no FPS label. kDisplayUpLeft = The engine displays a FPS label in the upper left corner. kDisplayUpRight = upper right corner. kDisplayDownRight = lower right corner. kDisplayDownLeft = lower left corner
 * Default: kDisplayNot.
 **/
@property kFPSLabelDisplayMode FPSLabelDisplayMode;

/**
 * The time the engine pauses after one drawView call. 
 * Default: 1/60 = 0.01666
 **/
@property NSTimeInterval animationInterval;

/**
 * Backbuffer width in pixel
 **/
@property GLint backingWidth;

/**
 * Backbuffer height in pixel
 **/
@property GLint backingHeight;

/**
 * The texture manager of the Engine.
 * You can use this class to load textures
 **/
@property (nonatomic, retain) VD_TextureManager *texManager;


/**
 * The label manager of the Engine. 
 * You can use this class to create Lables
 **/
@property (nonatomic, retain) VD_labelManager*lblManager;

/**
 * The panel manager of the Engine. 
 * You can use this class to create Panels, Animations and Particle emitters.
 **/
@property (nonatomic, retain) VD_PanelManager *panManager;

/**
 * The collision manager of the Engine.
 * You can use this class to have a simple collision detection.
 **/
@property (nonatomic, retain) VD_CollisionManager *colManager;

/**
 * The skyColor will be used as clear color for the View.
 * Default: r=0; g=0; b=0.5;
 * Range: 0.0 ... 1.0
 **/
@property (nonatomic, retain) VD_Color *skyColor;

/**
 * Starts the draw loop of the Engine.
 **/
- (void)startAnimation;

/**
 * Stops the draw loop of the Engine.
 **/
- (void)stopAnimation;

/**
 * This function foreces the engine to draw the scene on the view.
 **/
- (void)drawView;

/**
 * This function also forces the engine to draw the scene on the view, but it will ignore all waits and render calls!
 **/
- (void)forceRendering;

/**
 * Shedules a call to a given selector in a given class.
 * The function will be called every frame before the scene gets drawn.
 @param target The class where the function is
 @param selector The selector to the function. Create it with @selector(FUNCTIONAME);
 **/
- (void)scheduleRenderCallAt:(id)target withSelector:(SEL)selector;

/**
 * Deletes a scheduled render call.
 @param target The class where the function is
 @param selector The selector to the function. Create it with @selector(FUNCTIONAME);
 **/
- (void)deleteRenderCallAt:(id)target andSelector:(SEL)selector;

/**
 * This function will call the given selector one time when the engine will render the scene for the next time.
 @param target The class where the function is
 @param selector The selector to the function. Create it with @selector(FUNCTIONAME);
 **/
- (void)waitOneFrame:(id)target forSelector:(SEL)selector;

- (void)destroyFramebuffer;
- (BOOL)createFramebuffer;

- (id)initWithFrame:(CGRect)frame;

@end
